package controller;

import Util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.User;
import service.UserService;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/index")
    public String index(){
        //逻辑视图名
        return "index";
    }

    @RequestMapping("/login")
    public String login(User user){
        // 登录逻辑
        boolean flag = userService.login(user);
        if(flag){
            // /WEB-INF/login.jsp
            return "index";
        } else {
            return "login";
        }
    }

    @RequestMapping("/toregist")
    public String toRegist(){
        return "regist";
    }
    @RequestMapping("/register")
    public String register(User user){
        // 注册逻辑
        int result = userService.addUser(user);
        if(result>0){
            return "login";
        }else{
            return "regist";
        }
    }

    //全查询
//    @RequestMapping("/list/{currentPage}")
//    public String list(User user,@PathVariable("currentPage") int currentPage) {
//        //异步查询
//        //补全代码
//        return "list";
//    }
    @RequestMapping("/list/{currentPage}")
    public String list(User user, @PathVariable("currentPage") Integer currentPage, Model model) {
        Page page = new Page();
        int totalCount = userService.count(user);
        page.setTotalCount(totalCount);
        page.setCurrentPage(currentPage);
        page.setTotalPage();

        int offset = (currentPage - 1) * page.getPageSize();
        List<User> users = userService.findAll(user, offset, page.getPageSize());
        model.addAttribute("users", users);
        model.addAttribute("page", page);
        model.addAttribute("user", user);


        return "user/list";
    }

    @RequestMapping("/toadd")
    public String toAdd() {
        return "user/add";
    }

    @RequestMapping("/addSave")//注解里都需要加/
    public String addSave(User user) {
        int result = userService.addUser(user);
        if (result > 0) {
            return "redirect:/user/list/1";//重定向
        } else {
            return "user/add";
        }
    }

    @RequestMapping("/delete/{id}")
    public String deleteById(@PathVariable("id") Integer id) {
        int result = userService.deleteUserById(id);
        return result > 0 ? "redirect:/user/list/1" : "error";
    }

    @RequestMapping("/toupdate/{id}")//注解里都需要加/
    public String toUpdate(@PathVariable("id") Integer id, Model model) {
        User user = userService.findUserById(id);
        model.addAttribute("user", user);
        return "user/update";
    }

    @RequestMapping("/updateSave")
    public String updateSave(User user) {
        int result = userService.updateUser(user);
        if (result > 0) {
            return "redirect:/user/list/1";//重定向
        } else {
            return "user/update";
        }
    }

    @RequestMapping("/checkUserNameUnique")
    @ResponseBody
    public String checkUserNameUnique(@RequestParam("name") String name) {
        int count = userService.findUserByName(name);
        if (count > 0) {
            return "false";
        } else {
            return "true";
        }
    }
}